package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IFavoritesService;
import com.younion.business.IFavourableEntityService;
import com.younion.common.constants.SysConstants;
import com.younion.model.FavEntDetailedResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.API_FavourableEntityVO;

public class FavourableEntDetailGetConsumer extends AbstractRequestConsumer {
	private IFavourableEntityService favourableEntityService;
	private IFavoritesService favoritesService;
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getFavourableDetail(request,response);
	}
	
	public String getFavourableDetail(HttpServletRequest request,
			HttpServletResponse response){
		String user_token = request.getParameter("user_token");
		String fav_ent_id = request.getParameter("fav_ent_id");
		if (!HttpRequestUtils.checkNotEmptyParam("fav_ent_id", fav_ent_id,
				response)) {
			return null;
		}
		if (!HttpRequestUtils.checkNeedIntegerParam("fav_ent_id", fav_ent_id,
				response)) {
			return null;
		}
		int int_page = ApiConstant.API_DEFAULT_PAGE;
		ResultObject resultObject = new ResultObject();
		try {
			String img_url_pre = ConfigHelper.fastGetConfig("interface",
					"img_url_pre", "http://localhost:8080/younonmember/");
			Map paramMap = new HashMap();
			paramMap.put("id", fav_ent_id);
			List<API_FavourableEntityVO> apiFavEntList = (List<API_FavourableEntityVO>)favourableEntityService.getApiFavourableEntity(paramMap);
			API_FavourableEntityVO api_favourableEntityVO = apiFavEntList.get(0);
			FavEntDetailedResult favEntDetailedResult = new FavEntDetailedResult();
			if(api_favourableEntityVO!=null){
			favEntDetailedResult.setCat_id(api_favourableEntityVO.getCat_id());
			favEntDetailedResult.setFav_ent_id(fav_ent_id);
			favEntDetailedResult.setFav_name(api_favourableEntityVO.getName());
			favEntDetailedResult.setFav_detail(api_favourableEntityVO.getDetail());
			favEntDetailedResult.setNonactivatedPic(img_url_pre+ SysConstants.FavourableEntity_IMG_URL + 
					"/"+ api_favourableEntityVO.getId() + "/"+api_favourableEntityVO.getNonactivatedPic());
			favEntDetailedResult.setActivatedPic(img_url_pre+ SysConstants.FavourableEntity_IMG_URL + 
					"/"+ api_favourableEntityVO.getId() + "/"+api_favourableEntityVO.getActivatedPic());
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			favEntDetailedResult.setApply_date_from(
					(api_favourableEntityVO.getApplyBegDate()==null)?"":
					sf.format(api_favourableEntityVO.getApplyBegDate()));
			favEntDetailedResult.setApply_date_to(
					(api_favourableEntityVO.getApplyEndDate()==null)?"":
					sf.format(api_favourableEntityVO.getApplyEndDate()));
			favEntDetailedResult.setUse_date_from(
					(api_favourableEntityVO.getUseBegDate()==null)?"":
					sf.format(api_favourableEntityVO.getUseBegDate()));
			favEntDetailedResult.setUse_date_to(
					(api_favourableEntityVO.getUseEndDate()==null)?"":
					sf.format(api_favourableEntityVO.getUseEndDate()));
			favEntDetailedResult.setValid_time_from(api_favourableEntityVO.getValidTimeFrom());
			favEntDetailedResult.setValid_time_to(api_favourableEntityVO.getValidTimeTo());
			favEntDetailedResult.setShops(api_favourableEntityVO.getShops());
			Integer amount = api_favourableEntityVO.getAmount();
			favEntDetailedResult.setAmount((amount==null?"":amount).toString());
			favEntDetailedResult.setMoney(api_favourableEntityVO.getValue());
			
			if(api_favourableEntityVO.getParticipate_num()!=null){
				favEntDetailedResult.setParticipate_num(api_favourableEntityVO.getParticipate_num().toString());
				}else{
					favEntDetailedResult.setParticipate_num("0");
				}
			String fav_id = "";
			String is_favorites = "0";
			if(user_token!=null && !"".equals(user_token)){
			Map map = new HashMap();
			map.put("favourableEntity",Integer.valueOf(fav_ent_id));
			map.put("userToken", user_token);
			fav_id = favourableEntityService.selectAppliedNumByUserToken(map);
			map.put("favourId", Integer.valueOf(fav_ent_id));
			map.put("type", 2);
			is_favorites = Integer.valueOf(favoritesService.getCountByMap(map)).toString();
			}
			favEntDetailedResult.setIs_favorites(is_favorites);
			favEntDetailedResult.setFav_id(fav_id);
			favEntDetailedResult.setIsHot(api_favourableEntityVO.getIsHot().toString());
			favEntDetailedResult.setIsRecommend(api_favourableEntityVO.getIsRecommend().toString());
			favEntDetailedResult.setIsNew(api_favourableEntityVO.getIsNew().toString());
			favEntDetailedResult.setIsAuthenByMoblie(api_favourableEntityVO.getIsAuthenByMoblie());
			}
			resultObject.setResult(favEntDetailedResult);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}


	public void setFavourableEntityService(
			IFavourableEntityService favourableEntityService) {
		this.favourableEntityService = favourableEntityService;
	}

	public void setFavoritesService(IFavoritesService favoritesService) {
		this.favoritesService = favoritesService;
	}



	
}
